/* 
 *  Qactus - A Qt based OBS notifier
 *
 *  Copyright (C) 2010-2011 Sivan Greenberg <sivan@omniqueue.com>
 *  Copyright (C) 2010-2011 Javier Llorente <javier@opensuse.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "xmlreader.h"

xmlReader::xmlReader()
{
}

void xmlReader::read(QString& data, QString& details)
{
    QXmlStreamReader xml(data);

    while (!xml.atEnd() && !xml.hasError())
    {
        xml.readNext();

        if(xml.name()=="status")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef package = attrib.value("package");
                QStringRef code = attrib.value("code");

                qDebug() << "package:" << package.toString() << "status:" << code.toString();

                if(code.toString() == "unregistered_ichain_user")
                {
                    qDebug() << "Unregistered username!";
                }
                else
                {
                    data = code.toString();
                }
            }
        }

        if(xml.name()=="details")
        {
            if(xml.tokenType() != QXmlStreamReader::StartElement)
            {
                return;
            }
            xml.readNext();
            details = xml.text().toString();
//               qDebug() << "details:" << details;
        }
        else
        {
            details = "";
        }
    }

    if(xml.hasError())
    {
        qDebug() << "Error parsing XML!" << xml.errorString();
    }
}

void xmlReader::readSR(QString& data)
{
    QXmlStreamReader xml(data);
    int i=0;

    while (!xml.atEnd() && !xml.hasError())
    {
        xml.readNext();

        if(xml.name()=="collection")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef matches = attrib.value("matches");
                QStringRef code = attrib.value("code");
                reqNumber = matches.toString();

                qDebug() << "matches:" << reqNumber;

                if(code.toString() == "unregistered_ichain_user")
                {
                    qDebug() << "Unregistered username!";
                }
                else
                {
                    data = code.toString();
                }
            }

        }

        if(xml.name()=="request")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef id = attrib.value("id");
                reqID.append(id.toString());
//                    reqID = id.toString();
//                   qDebug() << "Request ID:" << reqID.at(i);
//                    qDebug() << "Request ID:" << reqID;
            }
        }

        if(xml.name()=="action")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef type = attrib.value("type");
                reqType.append(type.toString());
                qDebug() << "Action type:" << reqType[i];
            }
        }

        if(xml.name()=="source")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef project = attrib.value("project");
                QStringRef package = attrib.value("package");
                reqSourceProject.append(project.toString() + "/" + package.toString());
                qDebug() << "Source project:" << reqSourceProject[i];
                qDebug() << "Package:" << package.toString();
            }
        }

        if(xml.name()=="target")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef project = attrib.value("project");
                QStringRef package = attrib.value("package");
                reqTargetProject.append(project.toString());
                qDebug() << "Target project:" << reqTargetProject[i];
                qDebug() << "Package:" << package.toString();
            }
        }

        if(xml.name()=="state")
        {
            if(xml.isStartElement())
            {
                QXmlStreamAttributes attrib = xml.attributes();
                QStringRef state = attrib.value("name");
                reqState.append(state.toString());
                qDebug() << "State:" << reqState[i];

                QStringRef who = attrib.value("who");
                requester.append(who.toString());
                qDebug() << "Requester:" << requester[i];

                QStringRef when = attrib.value("when");
                reqDate.append(when.toString());
                qDebug() << "Date:" << reqDate[i];

            }
        }

        if(xml.name()=="description")
        {
            if(xml.tokenType() != QXmlStreamReader::StartElement)
            {
                return;
            }
            xml.readNext();
            QString description = xml.text().toString();
            qDebug() << "Description:" << description;
            i++;
            xml.readNextStartElement();
        }

    }

    if(xml.hasError())
    {
        qDebug() << "Error parsing XML!" << xml.errorString();
    }
}
